package com.kongjian.manage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kongjian.manage.common.R;
import com.kongjian.manage.mapper.DeptMapper;
import com.kongjian.manage.model.dto.NewDeptDTO;
import com.kongjian.manage.model.dto.UpdateDeptDTO;
import com.kongjian.manage.model.entity.DeptEntity;
import com.kongjian.manage.model.query.DeptQuery;
import com.kongjian.manage.model.vo.DeptVO;
import com.kongjian.manage.service.IDeptService;
import com.kongjian.manage.common.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author tangjunfeng
* @Classname DeptServiceImpl.java
* @Description 部门service
* @Date 2023-10-10
* @Version V1.0
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, DeptEntity> implements IDeptService {

    @Override
    public R<Page> queryPageDept(DeptQuery query) throws Exception {
        //构造分页构造器
        Page pageInfo = new Page(query.getPage(), query.getSize());
        LambdaQueryWrapper<DeptEntity> lambda = queryWhere(query);

        //执行查询
        Page result = this.page(pageInfo, lambda);
        return R.success(result);
    }

    @Override
    public DeptVO getByIdDept(Long id) throws Exception {
        DeptVO deptVO = new DeptVO();
        DeptEntity deptEntity = baseMapper.selectById(id);
        if(deptEntity != null) {
            BeanUtils.copyProperties(deptEntity,deptVO);
        }
        return deptVO;
    }

    @Override
    public void addDept(NewDeptDTO deptDTO) throws Exception {
        DeptEntity dept = new DeptEntity();
        BeanUtils.copyProperties(deptDTO,dept);
        baseMapper.insert(dept);
    }

    @Override
    public void modifyDept(Long id, UpdateDeptDTO deptDTO) throws Exception {
        DeptEntity dept = baseMapper.selectById(id);
        if(dept != null) {
                if(deptDTO.getFDeptId() != null) {
                dept.setFDeptId(deptDTO.getFDeptId());
                }
                if(deptDTO.getFDeptName() != null) {
                dept.setFDeptName(deptDTO.getFDeptName());
                }
                if(deptDTO.getFDeptCode() != null) {
                dept.setFDeptCode(deptDTO.getFDeptCode());
                }
                if(deptDTO.getFDeptFullname() != null) {
                dept.setFDeptFullname(deptDTO.getFDeptFullname());
                }
                if(deptDTO.getFDeptDwname() != null) {
                dept.setFDeptDwname(deptDTO.getFDeptDwname());
                }
                if(deptDTO.getFDeptDwcode() != null) {
                dept.setFDeptDwcode(deptDTO.getFDeptDwcode());
                }
        baseMapper.updateById(dept);
        }
    }

    @Override
    public void deleteDept(Long id) throws Exception {
        DeptEntity deptEntity = baseMapper.selectById(id);
        if(deptEntity == null) {
            throw new CustomException("id:" + id + "没有查到数据");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Integer countDept(DeptQuery query) {
        LambdaQueryWrapper<DeptEntity> lambda = queryWhere(query);
        return Math.toIntExact(baseMapper.selectCount(lambda));
    }

    private LambdaQueryWrapper<DeptEntity> queryWhere(DeptQuery query) {
        LambdaQueryWrapper<DeptEntity> lambda = new LambdaQueryWrapper<>();
        lambda.eq(query.getFDeptId() != null, DeptEntity::getFDeptId, query.getFDeptId());
        lambda.eq(query.getFDeptName() != null, DeptEntity::getFDeptName, query.getFDeptName());
        lambda.eq(query.getFDeptCode() != null, DeptEntity::getFDeptCode, query.getFDeptCode());
        lambda.eq(query.getFDeptFullname() != null, DeptEntity::getFDeptFullname, query.getFDeptFullname());
        lambda.eq(query.getFDeptDwname() != null, DeptEntity::getFDeptDwname, query.getFDeptDwname());
        lambda.eq(query.getFDeptDwcode() != null, DeptEntity::getFDeptDwcode, query.getFDeptDwcode());
        //添加排序条件
        lambda.orderByDesc(DeptEntity::getUpdateTime);
        return  lambda;
    }

}
